package com.yx.cloud.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.yx.cloud.MyApplication;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.TmallUnbinduserActivityBinding;
import com.yx.cloud.product.laser.tuyapublic.BluetoothScanDeviceBean;
import com.yx.cloud.utils.AliHttpRequest_Runnable;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.SharedPrefsUtil;
import com.yx.cloud.utils.ThreadUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.common.utils.Event;
//import com.yx.robot.sdk.req.BluetoothScanDeviceBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TmallUnbindUserActivity extends BaseActivity implements View.OnClickListener {
    private TmallUnbinduserActivityBinding binding;
    private String network, source, nickname, deviceType;
    private BluetoothScanDeviceBean deviceBean;
    private AliHttpRequest_Runnable runnable;
    private MyApplication application;
    private int unbindFrequency = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        application = (MyApplication) getApplicationContext();
        binding = TmallUnbinduserActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        if (runnable == null) {
            runnable = new AliHttpRequest_Runnable();
        }
        initData();
    }


    private void initData() {
        network = getIntent().getStringExtra("network");
        deviceType = getIntent().getStringExtra("device_type");
        if ("bluetooth".equals(network)) {
            deviceBean = (BluetoothScanDeviceBean) getIntent().getSerializableExtra("bluetoothData");
            source = getIntent().getStringExtra("source");
            nickname = getIntent().getStringExtra("nickname");
        }
        binding.tvBindUser.setText(nickname);

//        String a = getString(R.string.bind_Tmall_Genie_explanation);
//        String b = "很多";
//        binding.tvExplanation.setText(a + b);

        binding.ivALiunBindReturn.setOnClickListener(this);
        binding.btnUnbindUser.setOnClickListener(this);
        binding.btnUnbindNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = view.getId();
        if (id == R.id.iv_ALiunBind_return) {
            Intent_utils.backIntent(this);
        } else if (id == R.id.btn_unbindNext) {
            //下一步
            Intent intent = new Intent(this, SelectWIFIActivity.class);
            intent.putExtra("network", "bluetooth");
            intent.putExtra("source", "X5");
            intent.putExtra("bluetoothData", deviceBean);//保存的X5蓝牙信息
            intent.putExtra("device_type", deviceType);//设备型号
            startActivity(intent);
            finish();
        } else if (id == R.id.btn_unbindUser) {
            //解除绑定
            binding.tmallUnbindAccountLottie.playAnimation();
            binding.tmallUnbindAccountLottie.setVisibility(View.VISIBLE);
            if (runnable == null) {
                runnable = new AliHttpRequest_Runnable();
            }
            runnable.initialization("unbind_user", null);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_UNBIND_USER:
                if (event.isResult()) {
                    //解除绑定成功，并去重新绑定
                    binding.tmallUnbindAccountLottie.pauseAnimation();
                    binding.tmallUnbindAccountLottie.setVisibility(View.GONE);
                    //AppContext.get().setDeviceVariety("default");
                    SharedPrefsUtil.putValue(this, "TMALL_ACCESS_TOKEN", "tmall_access_token", "");
                    SharedPrefsUtil.putValue(this, "TMALL_USER_OPEN_ID", "tmall_user_open_id", "");
                    SharedPrefsUtil.putValue(this, "TMALL_AUTHENTICATION_TIME", "tmall_authentication_time", 0L);
                    SharedPrefsUtil.putValue(this, "TMALL_ALIGENIE_USER_INFO", "tmall_aligenie_user_info", "");//保存的天猫用户信息
                    Intent select = new Intent(this, BindTaobaoAccountActivity.class);
                    select.putExtra("network", "bluetooth");
                    select.putExtra("bluetoothData", deviceBean);
                    startActivity(select);
                    finish();
                } else {
                    //解除绑定失败
                    unbindFrequency++;
                    if (unbindFrequency <= 3) {
                        if (runnable == null) {
                            runnable = new AliHttpRequest_Runnable();
                        }
                        runnable.initialization("unbind_user", null);
                    } else {
                        binding.tmallUnbindAccountLottie.pauseAnimation();
                        binding.tmallUnbindAccountLottie.setVisibility(View.GONE);
                        ToastUtil.show(TmallUnbindUserActivity.this, event.getContent() + "", Toast.LENGTH_SHORT);
                    }
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (runnable != null) {
            ThreadUtils.getInstance().removeThread(runnable);
        }
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
